﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//创建一个楼梯
public class CreateStairs : EditorWindow
{
    static CreateStairs creatStairsWindow;
    [MenuItem("Window/CreateStairs")]
    static void ShowWindow()
    {
        creatStairsWindow = GetWindow<CreateStairs>();
        creatStairsWindow.Show();
    }
    float rectWidth, rectHeight;
    int rectCount;
    float coincide;
    Sprite spriteRect;
    Color color;
    private void OnGUI()
    {
        rectHeight = EditorGUILayout.Slider("矩形高度：", rectHeight, 0.5f, 5f);
        rectWidth = EditorGUILayout.Slider("矩形宽度：", rectWidth, 1f, 9f);
        rectCount = EditorGUILayout.IntSlider("矩形个数：",rectCount, 2, 10);
        coincide = EditorGUILayout.Slider("重合比例：", coincide, 0.1f, 0.4f);
        spriteRect = EditorGUILayout.ObjectField("图片：", spriteRect, typeof(Sprite),false) as Sprite;
        color = EditorGUILayout.ColorField("颜色：", color);
        if (GUILayout.Button("创建梯子"))
        {
            GameObject stairs = new GameObject("Staris");
            Vector3 rectPos = Vector3.zero;
            float coinWid = coincide * rectWidth / 2f;
            for (int i = 0; i < rectCount; i++)
            {
                GameObject rect = new GameObject("Rect" + i);
                rect.AddComponent<BoxCollider2D>();
                rect.transform.localScale = new Vector3(rectWidth, rectHeight, 1f);
                SpriteRenderer sr = rect.AddComponent<SpriteRenderer>();
                sr.color = color;
                sr.sprite = spriteRect;
                rect.transform.parent = stairs.transform;
                rect.transform.localPosition = rectPos;
                rectPos += new Vector3(rectWidth - coinWid, rectHeight, 0);
            }
        }
    }
}
